import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

import sys
# To use random you must put the Python
# standard library on the path
sys.path.append('c:\\Python24\\Lib')

from System.Timers import Timer
from System.Drawing import Color, Font, FontStyle, Point, Pen, Bitmap, Graphics
from System.Windows.Forms import Application, BorderStyle, Button, CheckBox, Form, Label, Panel, RadioButton

class LinesPanel(Panel):
    def __init__(self, width=600):
        self.Width = width
        self.Height = width
        
        self.Location = Point(50, 50)

        self.BorderStyle = BorderStyle.FixedSingle

        self.UserControl = True
        self.DoubleBuffered = True

class LinesForm(Form):
    def __init__(self, game):
        self.game = game

        self.Text = "Lines Game"

        self.Width = game.width
        self.Height = game.width

        self.BackColor = Color.LightSlateGray

        self.panel = LinesPanel()

        self.panel.Paint += self.draw

        self.KeyPress += self.key_pressed
        
        self.Controls.Add(self.panel)

        #start the timer
        self.timer = Timer()
        self.timer.Elapsed += self.step
        self.timer.Interval = 10
        self.timer.Enabled = True
   
    def step(self, sender, e):
        self.game.step(sender, e)
        self.draw(sender, e)

    def key_pressed(self, sender, e):
        self.game.key_pressed(e.KeyChar)

    def draw(self, sender, e):
        self.BackBuffer = Bitmap(self.panel.Width, self.panel.Height);
        self.BufferGraphics = Graphics.FromImage(self.BackBuffer);

        self.game.draw(self.BufferGraphics)

        graphics = self.panel.CreateGraphics()

        graphics.DrawImageUnscaled(self.BackBuffer, 0, 0);

        graphics.Dispose()

class Directions:
    up, right, down, left = range(4)

    def move(point, direction):
        if direction == Directions.right:
            return Point(point.X + 1, point.Y)
        if direction == Directions.down:
            return Point(point.X, point.Y + 1)
        if direction == Directions.left:
            return Point(point.X - 1, point.Y)
        if direction == Directions.up:
            return Point(point.X, point.Y - 1)

    move = staticmethod(move)

class Line:
    def __init__(self, length, point):
        self.direction = Directions.up
        self.points = []

        for i in range(0, length):
            self.points.append(point)

    def move(self):
        self.points.insert(0, Directions.move(self.points[0], self.direction))
        self.points.pop()

    def change_direction(self, direction):
        self.direction = direction

class LinesGame:
    def __init__(self, width=700, length=100):
        self.width = width

        self.line = Line(length, Point(self.width/2, self.width/2))

    def step(self, sender, e):
        self.line.move()

    def draw(self, graphics):
        graphics.Clear(Color.White)
        pen = Pen(Color.Blue, 1)

        p1 = self.line.points[0]
        for i,p2 in enumerate(self.line.points):
            if i != 0:
                graphics.DrawLine(pen, p1, p2)
                p1 = p2

    def key_pressed(self, key):
        print '%c' % key
        if key == 'w':
            self.line.change_direction(Directions.up)
        if key == 's':
            self.line.change_direction(Directions.down)
        if key == 'a':
            self.line.change_direction(Directions.left)
        if key == 'd':
            self.line.change_direction(Directions.right)
            
        
form = LinesForm(LinesGame())
Application.Run(form)
